package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.quality.domain.QualityBadSample;
import com.pureut.quality.domain.QualityInspectionDocument;
import com.pureut.quality.domain.QualitySampleInformation;
import com.pureut.quality.domain.QualityTemporary;
import com.pureut.quality.domain.dto.QualityBadSampleDto;
import com.pureut.quality.mapper.QualityBadSampleMapper;
import com.pureut.quality.mapper.QualityInspectionDocumentMapper;
import com.pureut.quality.mapper.QualitySampleInformationMapper;
import com.pureut.quality.mapper.QualityTemporaryMapper;
import com.pureut.quality.service.QualityBadSampleService;
import com.pureut.quality.service.QualityInspectionDocumentService;
import com.pureut.quality.service.QualitySampleInformationService;
import com.pureut.quality.service.QualityTemporaryService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 不良样品实现层
 * Author:C
 * Date:2023/02/06 17:31
 * @DESC
 */
@Service
public class QualityBadSampleServiceImpl extends ServiceImpl<QualityBadSampleMapper, QualityBadSample> implements QualityBadSampleService {

    @Resource
    QualityBadSampleMapper qualityBadSampleMapper;

    @Resource
    QualitySampleInformationMapper qualitySampleInformationMapper;

    @Resource
    QualityTemporaryMapper qualityTemporaryMapper;

    @Resource
    QualityTemporaryService qualityTemporaryService;

    @Resource
    QualityInspectionDocumentMapper qualityInspectionDocumentMapper;

    /**
     * 获取检验数量
     *
     * @param inspectionSheetNo
     * @return
     */
    @Override
    public int getNum(String inspectionSheetNo) {
        Long deptId = SecurityUtils.getDeptId();
        return qualityBadSampleMapper.getNum(inspectionSheetNo, deptId);
    }


    /**
     * 列表
     *
     * @param inspectionSheetNo
     * @return
     */
    @Override
    public List<QualityBadSampleDto> getList(String inspectionSheetNo) {
        //检验结果
        List<SysDictData> resultsArray = DictUtils.getDictCache("quality_inspection_results");
        Map<String, String> resultsMap = resultsArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<QualityBadSampleDto> list = qualityBadSampleMapper.getList(inspectionSheetNo);
        for (QualityBadSampleDto entity : list) {
            entity.setInspectionResultsDict(resultsMap.get(entity.getInspectionResults()));
        }
        return list;
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id, String inspectionSheetNo) {
        QualityBadSample byId = getById(id);
        //同事删除样本信息数据
        List<QualitySampleInformation> qualitySampleInformationList = qualitySampleInformationMapper.selectList(new QueryWrapper<QualitySampleInformation>().lambda().eq(QualitySampleInformation::getSampleNumber, byId.getSampleNumber()));
        if (qualitySampleInformationList.size() > 0) {
            qualitySampleInformationMapper.deleteBatchIds(qualitySampleInformationList);
        }
        qualityBadSampleMapper.deleteById(byId);
        //修改对应物料检验状态
        QualityTemporary qualityTemporary = qualityTemporaryMapper.selectOne(new QueryWrapper<QualityTemporary>().lambda().eq(QualityTemporary::getMaterialNumber, byId.getSampleNumber()).eq(QualityTemporary::getInspectionSheetNo,inspectionSheetNo));
        qualityTemporary.setInspectionMark(2);
        qualityTemporary.setInspectionResults(0);
        qualityTemporary.setInspectionBy("");
        //获取检验过的物料条数
        int num = getNum(inspectionSheetNo);
        if (num == 0) {
            //修改检验单据状态为待检验
            QualityInspectionDocument qualityInspectionDocument = qualityInspectionDocumentMapper.selectOne(new QueryWrapper<QualityInspectionDocument>().lambda().eq(QualityInspectionDocument::getInspectionSheetNo, inspectionSheetNo));
//            qualityInspectionDocument.setStatus(1);
            qualityInspectionDocument.setJudgmentGrade(0);
            qualityInspectionDocument.setInspectionResults(0);
            qualityInspectionDocument.setActualInspectionQuantity(0L);
            qualityInspectionDocument.setBadSampleNum(0L);
            qualityInspectionDocumentMapper.updateById(qualityInspectionDocument);
        }
        return qualityTemporaryService.updateById(qualityTemporary);
    }
}
